package com.sinaapp.gavinzhang.jianzhioffer;

/**
 * 偶数在前，奇数在后
 * 类似插入排序
 * Created by gavin on 15-9-4.
 */
public class ReOrderArray {
    public static void main(String[] args)
    {
        int[] tmp = {1,2,3,4,5,6,7};
        reOrderArray(tmp);
        int i = 0;
        System.out.println("");
    }

    public static void reOrderArray(int [] array) {
        if (array == null || array.length==1 || array.length==0)return;
        int first=0;//找偶数

        while (first < array.length && array[first]%2 ==1)first++;
        for (int i = first+1; i < array.length; i++) {
            if (array[i]%2 ==1)
            {
                for (int j = i; j >0 ; j--) {
                    if (array[j-1]%2==0)
                    {
                        array[j] ^=array[j-1];
                        array[j-1] ^=array[j];
                        array[j] ^=array[j-1];
                    }else {
                        break;
                    }
                }
            }
        }
    }
}
